package com.yuanheng100.ems4j.mapper;

import com.yuanheng100.ems4j.model.Ems4jEmployee;
import com.yuanheng100.ems4j.model.Ems4jRole;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.caches.ehcache.EhcacheCache;

import java.util.List;

@CacheNamespace(implementation = EhcacheCache.class)
public interface Ems4jRoleMapper
{

    /**
     * 新增角色
     *
     * @param record
     * @return
     */
    @Insert({
            "INSERT INTO ems4j_role (roleid, rolename, ",
            "appid, description, ",
            "createtime, createuserid, ",
            "deletedate)",
            "VALUES (#{roleid,jdbcType=INTEGER}, #{rolename,jdbcType=VARCHAR}, ",
            "#{appid,jdbcType=INTEGER}, #{description,jdbcType=VARCHAR}, ",
            "#{createtime,jdbcType=TIMESTAMP}, #{createuserid,jdbcType=INTEGER}, ",
            "#{deletedate,jdbcType=DATE})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "roleid")
    int insert(Ems4jRole record);

    /**
     * 修改角色信息
     *
     * @param record
     * @return
     */
    @Update({
            "UPDATE ems4j_role",
            "SET rolename = #{rolename,jdbcType=VARCHAR},",
            "appid = #{appid,jdbcType=INTEGER},",
            "description = #{description,jdbcType=VARCHAR},",
            "createtime = #{createtime,jdbcType=TIMESTAMP},",
            "createuserid = #{createuserid,jdbcType=INTEGER},",
            "deletedate = #{deletedate,jdbcType=DATE}",
            "WHERE roleid = #{roleid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Ems4jRole record);

    /**
     * 对员工增加角色
     *
     * @param roleid
     * @param userid
     * @return
     */
    @Insert({
            "INSERT INTO ems4j_emp_role (userid, roleid)",
            "VALUES(#{userid,jdbcType=VARCHAR},#{roleid,jdbcType=INTEGER})"
    })
    int addRoleToEmployee(@Param("roleid") int roleid, @Param("userid") int userid);

    /**
     * 对员工删除角色
     *
     * @param roleid
     * @param userid
     * @return
     */
    @Delete({
            "DELETE FROM ems4j_emp_role",
            "WHERE userid = #{userid,jdbcType=VARCHAR}",
            "AND roleid = #{roleid,jdbcType=INTEGER}"
    })
    int removeRoleFromEmployee(@Param("roleid") int roleid, @Param("userid") int userid);

    @Select({
            "SELECT",
            "er.roleid, er.rolename, er.appid, er.description, er.createtime, er.createuserid, er.deletedate",
            "FROM ems4j_role er",
            "LEFT JOIN ems4j_emp_role eer ON er.roleid = eer.roleid",
            "WHERE eer.userid = #{userid,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "roleid", property = "roleid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "rolename", property = "rolename", jdbcType = JdbcType.VARCHAR),
            @Result(column = "appid", property = "appid", jdbcType = JdbcType.INTEGER),
            @Result(column = "description", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "createuserid", property = "createuserid", jdbcType = JdbcType.INTEGER),
            @Result(column = "deletedate", property = "deletedate", jdbcType = JdbcType.DATE)
    })
    List<Ems4jRole> selectByUserid(int userid);

    @Select({
            "SELECT",
            "ee.userid, ee.name, ee.department, ee.mobile, ee.email, ee.position, ee.password, ee.idcardno, ee.entrydate, ",
            "ee.deletedate, ee.empstatus, ee.appids, ee.weixinid, ee.weixinavatarmediaid, ee.dingtel, ee.dingworkplace, ",
            "ee.dingjobnumber, ee.dingissenior, ee.dingisleaderindepts",
            "FROM ems4j_employee ee",
            "LEFT JOIN ems4j_emp_role eer ON ee.userid = eer.userid",
            "WHERE eer.roleid = #{roleid,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "userid", property = "userid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "department", property = "department", jdbcType = JdbcType.INTEGER),
            @Result(column = "mobile", property = "mobile", jdbcType = JdbcType.BIGINT),
            @Result(column = "email", property = "email", jdbcType = JdbcType.VARCHAR),
            @Result(column = "position", property = "position", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.CHAR),
            @Result(column = "idcardno", property = "idcardno", jdbcType = JdbcType.CHAR),
            @Result(column = "entrydate", property = "entrydate", jdbcType = JdbcType.DATE),
            @Result(column = "deletedate", property = "deletedate", jdbcType = JdbcType.DATE),
            @Result(column = "empstatus", property = "empstatus", jdbcType = JdbcType.SMALLINT),
            @Result(column = "appids", property = "appids", jdbcType = JdbcType.VARCHAR),
            @Result(column = "weixinid", property = "weixinid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "weixinavatarmediaid", property = "weixinavatarmediaid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "dingtel", property = "dingtel", jdbcType = JdbcType.INTEGER),
            @Result(column = "dingworkplace", property = "dingworkplace", jdbcType = JdbcType.VARCHAR),
            @Result(column = "dingjobnumber", property = "dingjobnumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "dingissenior", property = "dingissenior", jdbcType = JdbcType.SMALLINT),
            @Result(column = "dingisleaderindepts", property = "dingisleaderindepts", jdbcType = JdbcType.VARCHAR)
    })
    List<Ems4jEmployee> selectEmployeeByRoleid(int roleId);

    /**
     * 获取应用的所有角色
     *
     * @return
     */
    @Select({
            "SELECT * FROM ems4j_role WHERE appId = #{appId,jdbcType=INTEGER}"
    })
    List<Ems4jRole> selectRoleListByAppId(int appId);

    @Select({
            "SELECT",
            "roleid, rolename, appid, description, createtime, createuserid, deletedate",
            "FROM ems4j_role",
            "WHERE roleid = #{roleid,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "roleid", property = "roleid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "rolename", property = "rolename", jdbcType = JdbcType.VARCHAR),
            @Result(column = "appid", property = "appid", jdbcType = JdbcType.INTEGER),
            @Result(column = "description", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "createuserid", property = "createuserid", jdbcType = JdbcType.INTEGER),
            @Result(column = "deletedate", property = "deletedate", jdbcType = JdbcType.DATE)
    })
    Ems4jRole selectByPrimaryKey(Integer roleid);

    @Select({
            "SELECT",
            "roleid, rolename, appid, description, createtime, createuserid, deletedate",
            "FROM ems4j_role"
    })
    @Results({
            @Result(column = "roleid", property = "roleid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "rolename", property = "rolename", jdbcType = JdbcType.VARCHAR),
            @Result(column = "appid", property = "appid", jdbcType = JdbcType.INTEGER),
            @Result(column = "description", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "createuserid", property = "createuserid", jdbcType = JdbcType.INTEGER),
            @Result(column = "deletedate", property = "deletedate", jdbcType = JdbcType.DATE)
    })
    List<Ems4jRole> selectAll();

}